﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon;
using CDMS.Common;
using CDMS.Entities;
using CDMS.BLL;
using DevExpress.XtraGrid.Columns;

namespace CDMS.Forms
{
    public partial class frmCustomerSearch : XtraForm
    {
        Customer _customer;

        public Customer Customer
        {
            get { return _customer; }
            set { _customer = value; }
        }

        public frmCustomerSearch()
        {
            InitializeComponent();
            this.layoutControl1.AllowCustomizationMenu = false;

            this.DialogResult = DialogResult.Cancel;
            AddColumn();
            LookUpEdit_CotTimKiem();
            LoadData();
        }

        private void AddColumn()
        {
            //Hide row header GroupPanel
            this.gridView1.OptionsView.ShowGroupPanel = false;
            //Hide row header
            this.gridView1.OptionsView.ShowIndicator = false;

            //Hide column header
            //this.gridView1.OptionsView.ShowColumnHeaders = false;

            //Set selection mode to fullrow
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

            this.gridView1.Columns.Clear();

            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_ID", "", "", 0, false, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Code", "Mã khách hàng", "", 100, true, true);
            FormGlobals.GridView_AddColumn(gridView1, new GridColumn(), "Customer_Name", "Tên khách hàng", "", 300, true, true);



        }
        void LookUpEdit_CotTimKiem()
        {
            LookUpEdit lup = lookUpEditCotTimKiem;

            //Tạo bảng tạm
            DataTable dt = new DataTable();
            dt.Columns.Add("Value");
            dt.Columns.Add("Name");

            //Thêm dữ liệu lấy từ danh sách cột trên gridview
            DataRow dr;
            foreach (GridColumn item in gridView1.Columns)
            {
                if (item.Visible)
                {
                    dr = dt.NewRow();
                    dr[0] = item.FieldName;
                    dr[1] = item.Caption;
                    dt.Rows.Add(dr);
                }
            }
            //Sắp xếp lại bảng
            DataView dv = dt.DefaultView;
            dv.Sort = "Name asc";
            DataTable sortedDT = dv.ToTable();

            lup.Properties.DataSource = sortedDT;
            lup.Properties.DisplayMember = "Name";
            lup.Properties.ValueMember = "Value";

            // Thêm thông tin hai cột
            DevExpress.XtraEditors.Controls.LookUpColumnInfoCollection coll = lup.Properties.Columns;
            coll.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Value", 0, "Giá trị"));
            coll.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", 0, "Tên"));

            lup.Properties.Columns[0].Visible = false;
            lup.Properties.ShowHeader = false;

            lup.EditValue = "Customer_Name";
        }
        void LoadData()
        {
            FormGlobals.ShowWaitForm();
            Customer_BLL Customer_BLL = new Customer_BLL();
            DataTable dt = Customer_BLL.Search(textEditKeySearch.Text, lookUpEditCotTimKiem.EditValue.ToString(), checkEditTimKyTuDau.Checked);
            gridControl1.DataSource = dt;
            gridControl1.ForceInitialize();
            FormGlobals.CloseWaitForm();
        }

        private void barButtonItemChon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            _customer = new Entities.Customer();
            _customer.Customer_ID = (int)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]);
            _customer.Customer_Code = (string)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1]);
            _customer.Customer_Name = (string)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2]);

            this.DialogResult = DialogResult.OK;
        }

        private void barButtonItemThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItemSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            barButtonItemChon.PerformClick();
        }

        private void textEditKeySearch_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void lookUpEditCotTimKiem_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void checkEditTimKyTuDau_CheckedChanged(object sender, EventArgs e)
        {
            LoadData();
        }



    }
}